package com.shengyin.ec.security;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;


public class User implements UserDetails {


	/**
	 * 
	 */
	private static final long serialVersionUID = -5423894946851309630L;
	/**
	 * 
	 */
	protected String realname;
	protected String username;
	protected String password;
	protected boolean enabled;
	protected GrantedAuthority[] authorities;
	 
	public Collection<GrantedAuthority> getAuthorities() {
		return Arrays.asList(this.authorities);
	}

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	
	public boolean isEnabled() {
		return this.enabled;
	}
	
	
	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	
	
	 protected void setAuthorities(GrantedAuthority[] authorities) {
	        Assert.notNull(authorities,
	            "Cannot pass a null GrantedAuthority array");

	        // Ensure array iteration order is predictable (as per UserDetails.getAuthorities() contract and SEC-xxx)
	        SortedSet<GrantedAuthority> sorter = new TreeSet<GrantedAuthority>();

	        for (int i = 0; i < authorities.length; i++) {
	            Assert.notNull(authorities[i],
	                "Granted authority element " + i
	                + " is null - GrantedAuthority[] cannot contain any null elements");
	            sorter.add(authorities[i]);
	        }

	        this.authorities = sorter.toArray(new GrantedAuthority[sorter
	                .size()]);
	    }
	 
	 
	 public void setAuthoritiesByString(String[] authorities) {
			int length = authorities.length;
			List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
			for (int i = 0; i < length; i++) {
				GrantedAuthorityImpl grantedAuthorityImpl = new GrantedAuthorityImpl(
						authorities[i]);
				grantedAuthorities.add(grantedAuthorityImpl);
			}

			this.authorities = grantedAuthorities
					.toArray(new GrantedAuthority[authorities.length]);
		}
	 public String getAuthoritiesString(){
	    	
	    	List<String> authorities = new ArrayList<String>();   
	        for(GrantedAuthority authority : this.getAuthorities()) {   
	            authorities.add(authority.getAuthority());   
	        }   
	        return StringUtils.join(authorities.toArray(), ",");   
	    }
}
